package prassein.rsm.web;

import java.io.File;
import java.util.Date;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.common.utils.SystemBackup;
import prassein.rsm.common.utils.SystemRestore;
import prassein.rsm.common.utils.ZipCompressor;
import prassein.rsm.common.utils.ZipUncompressor;
import prassein.rsm.service.base.AuditManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "success",location="systemManager.jsp"), 
	@Result(name = "error",location="login.jsp"),
	@Result(name = "input",location="systemManager.jsp")})
public class SystemManagerAction extends ActionSupport {
	private static final long serialVersionUID = 4960800769845732942L;
	
	private static int BACKUPING = 0;
	private static int RESTOREING = 0;

	private static int BACKUP = 1;
	private static int RESTORE = 2;
	
	private static int BACKUP_SUCCESS = 1;
	private static int BACKUP_FAILED = 2;
	private static int RESTORE_SUCCESS = 3;
	private static int RESTORE_FAILED = 4;
	
	@Autowired
	private AuditManager auditManager;
	
	public int managerType;
	
	public String backupLocation;
	
	public String restoreLocation;
	
	public int resule;
	
	@Override 
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {
			if (managerType == BACKUP) {
				try {
					@SuppressWarnings("deprecation")
					File file = new File(backupLocation, new Date().toLocaleString().replace(" ", "_").replace(":", "_"));
					
					if (!file.exists() && BACKUPING == 0) {
						BACKUPING = 1;
						file.mkdirs();
						
						SystemBackup backup = new SystemBackup();
						backup.backupDB(file.getAbsolutePath());
						backup.backupResumeFiles(file.getAbsolutePath());
						
						ZipCompressor zip = new ZipCompressor(file.getAbsolutePath().replace(file.getName(), "")+ "/" + file.getName() + ".zip");
						zip.compress(file.getAbsolutePath());
						
						FileUtils.deleteDirectory(file);
						
						auditManager.save(session.get(Constants.USERNAME).toString(), "", "系统备份");
					}
					
					resule = BACKUP_SUCCESS;
				} catch (Exception e) {
					
					resule = BACKUP_FAILED;
				} finally {
					BACKUPING = 0;
				}
			} else if (managerType == RESTORE) {
				try {
					@SuppressWarnings("deprecation")
					File backupFile = new File("webapps/resume", new Date().toLocaleString().replace(" ", "_").replace(":", "_") + "_backup");
					
					if (!backupFile.exists() && RESTOREING == 0) {
						RESTOREING = 1;
						backupFile.mkdirs();
						
						File uploadFolder = new File("webapps/resume/upload");
						File tempFile = new File("webapps/resume", new Date().toLocaleString().replace(" ", "_").replace(":", "_") + "_temp");
						tempFile.mkdirs();
						
						ZipUncompressor unzip = new ZipUncompressor(restoreLocation);
						unzip.uncompress(tempFile.getAbsolutePath());
						File sysBackupFile = tempFile.listFiles()[0];
						File resumeFiles = new File(sysBackupFile, "resumeFiles");
						File mysqlBackup = new File (sysBackupFile, "mysql.backup");
						
						SystemRestore sysRestore = new SystemRestore();
						sysRestore.restoreDB(mysqlBackup.getAbsolutePath());
						
						SystemBackup backup = new SystemBackup();
						backup.backupResumeFiles(backupFile.getAbsolutePath());
						
						FileUtils.copyDirectory(uploadFolder, backupFile);
						FileUtils.deleteDirectory(uploadFolder);
						resumeFiles.renameTo(uploadFolder);
						
						FileUtils.deleteDirectory(tempFile);
						FileUtils.deleteDirectory(backupFile);
						
						auditManager.save(session.get(Constants.USERNAME).toString(), "", "系统还原");
						
						resule = RESTORE_SUCCESS;
					}
				} catch (Exception e) {
					resule = RESTORE_FAILED;
				} finally {
					RESTOREING = 0;
				}
			}
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public int getManagerType() {
		return managerType;
	}

	public void setManagerType(int managerType) {
		this.managerType = managerType;
	}

	public String getRestoreLocation() {
		return restoreLocation;
	}

	public void setRestoreLocation(String restoreLocation) {
		this.restoreLocation = restoreLocation;
	}

	public int getResule() {
		return resule;
	}

	public void setResule(int resule) {
		this.resule = resule;
	}

	public AuditManager getAuditManager() {
		return auditManager;
	}

	public void setAuditManager(AuditManager auditManager) {
		this.auditManager = auditManager;
	}
}
